package com.chuanjiabao.wisdom.ui.activity;

import android.content.ClipboardManager;
import android.content.Context;
import android.content.Intent;
import android.databinding.DataBindingUtil;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.view.View;

import com.chuanjiabao.wisdom.R;
import com.chuanjiabao.wisdom.bean.Constants;
import com.chuanjiabao.wisdom.bean.FamilyDeatilCallbackBean;
import com.chuanjiabao.wisdom.bean.InheritCallBackBean;
import com.chuanjiabao.wisdom.databinding.ActivitySuccessorSelectBinding;
import com.chuanjiabao.wisdom.listener.OnBHClickListener;
import com.chuanjiabao.wisdom.ui.BaseActivity;
import com.chuanjiabao.wisdom.util.GlideUtil;
import com.chuanjiabao.wisdom.util.HTTPAPI;
import com.chuanjiabao.wisdom.util.StringUtil;
import com.chuanjiabao.wisdom.util.ToastUtil;
import com.chuanjiabao.wisdom.util.dialog.SecretDoneDialog;
import com.chuanjiabao.wisdom.util.dialog.SecretMakeDialog;
import com.lzy.okgo.callback.StringCallback;

import org.json.JSONException;
import org.json.JSONObject;

import okhttp3.Call;
import okhttp3.Response;

/**
 * 传承人选择
 */
public class SuccessorSelectActivity extends BaseActivity{

    private ActivitySuccessorSelectBinding binding;
    private InheritCallBackBean.DataBean dataBean;
    private String mHomeId = "", mUserId = "";

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        binding = DataBindingUtil.setContentView(this,R.layout.activity_successor_select);
        binding.setActivity(this);
        initView();
    }

    private void initView() {
        dataBean = (InheritCallBackBean.DataBean) getIntent().getSerializableExtra("data");

        InheritCallBackBean.DataListBean item = dataBean.getHomes().get(0);
        mHomeId = item.getId();

        if (TextUtils.isEmpty(item.getAvatar())) {
            binding.tvModuleNameHint.setVisibility(View.VISIBLE);
            binding.ivModuleNameHead.setVisibility(View.GONE);
            binding.tvModuleNameHint.setText(item.getTruename().substring(0, 1));
        } else {
            binding.tvModuleNameHint.setVisibility(View.GONE);
            binding.ivModuleNameHead.setVisibility(View.VISIBLE);
            GlideUtil.setCircleMethod(this, item.getAvatar(), binding.ivModuleNameHead);
        }
        binding.tvModuleName.setText(item.getTruename());

        if (dataBean.getInherit() == 3) {
            binding.tvModuleSelectSuccessor.setVisibility(View.GONE);
            binding.tvModuleSuccessorName.setText(item.getInherit_user_truename());
            binding.tvModuleCreateKey.setText("正在传承中，查看传承密钥");
            if (TextUtils.isEmpty(item.getInherit_user_avatar())) {
                binding.tvModuleNameHint1.setVisibility(View.VISIBLE);
                binding.ivModuleSuccessorHead.setVisibility(View.GONE);
                binding.tvModuleNameHint1.setText(item.getInherit_user_truename().substring(0, 1));
            } else {
                binding.tvModuleNameHint1.setVisibility(View.GONE);
                binding.ivModuleSuccessorHead.setVisibility(View.VISIBLE);
                GlideUtil.setCircleMethod(this, item.getInherit_user_avatar(), binding.ivModuleSuccessorHead);
            }
        }

        binding.rvModuleLeft.setOnClickListener(this);
        binding.tvModuleCreateKey.setOnClickListener(this);
        binding.llModuleSelect.setOnClickListener(this);
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()){
            case R.id.rvModuleLeft:
                onBackPressed();
                break;
            case R.id.llModuleSelect:
                if (dataBean.getInherit() != 3) {
                    InheritCallBackBean.DataListBean item = dataBean.getHomes().get(0);
                    Intent intent = new Intent(SuccessorSelectActivity.this, SuccessorListActivity.class);
                    intent.putExtra("id", item.getId());
                    startActivityForResult(intent, Constants.REQUEST_CODE);
                }
                break;
            case R.id.tvModuleCreateKey:
                if (dataBean.getInherit() == 3) {
                    InheritCallBackBean.DataListBean item = dataBean.getHomes().get(0);
                    SecretDoneDialog dialog = new SecretDoneDialog(this, item.getSecret());
                    dialog.show();
                    dialog.setOnBHClickListener(new OnBHClickListener() {
                        @Override
                        public void onConfirmClick() {
                            ClipboardManager cm = (ClipboardManager) getSystemService(Context.CLIPBOARD_SERVICE);
                            //noinspection deprecation
                            cm.setText(item.getSecret());
                            ToastUtil.showToast("复制成功！");
                        }
                    });
                } else {
                    if (TextUtils.isEmpty(mUserId)) {
                        ToastUtil.showToast("请选择传承人");
                        return;
                    }

                    getSecret();
                }
                break;
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == RESULT_OK && requestCode == Constants.REQUEST_CODE) {
            FamilyDeatilCallbackBean.DataBean.MembersBean item =
                    (FamilyDeatilCallbackBean.DataBean.MembersBean) data.getSerializableExtra("module_bean");

            mUserId = item.getD_user_id() + "";
            binding.tvModuleSelectSuccessor.setVisibility(View.GONE);
            binding.tvModuleSuccessorName.setText(item.getD_name());
            if (TextUtils.isEmpty(item.getD_head_pic())) {
                binding.tvModuleNameHint1.setVisibility(View.VISIBLE);
                binding.ivModuleSuccessorHead.setVisibility(View.GONE);
                binding.tvModuleNameHint1.setText(item.getD_name().substring(0, 1));
            } else {
                binding.tvModuleNameHint1.setVisibility(View.GONE);
                binding.ivModuleSuccessorHead.setVisibility(View.VISIBLE);
                GlideUtil.setCircleMethod(this, item.getD_head_pic(), binding.ivModuleSuccessorHead);
            }
        }
    }

    private void showSuccessAnim(String secret) {
        SecretMakeDialog dialog = new SecretMakeDialog(this, secret);
        dialog.show();
        dialog.setOnBHClickListener(new OnBHClickListener() {
            @Override
            public void onConfirmClick() {
                ClipboardManager cm = (ClipboardManager) getSystemService(Context.CLIPBOARD_SERVICE);
                //noinspection deprecation
                cm.setText(secret);
                ToastUtil.showToast("复制成功！");
            }
        });
    }

    private void getSecret() {
        showProgressDialog();
        HTTPAPI.getInstance().getInheritSecret(mHomeId, mUserId, new StringCallback() {
            @Override
            public void onSuccess(String s, Call call, Response response) {
                cancelProgressDialog();
                if (!StringUtil.isFail(s)) {
                    StringUtil.showFail(SuccessorSelectActivity.this, s);
                } else {
                    dataBean.setInherit(3);
                    binding.tvModuleCreateKey.setText("正在传承中，查看传承密钥");
                    try {
                        JSONObject json = new JSONObject(s);
                        if (json.has("data")) {
                            JSONObject obj = json.getJSONObject("data");
                            if (obj.has("secret")) {
                                showSuccessAnim(obj.optString("secret"));
                            }
                        }
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                }
            }
        });
    }

}
